<?php

namespace Api\Controller;

use Common\Controller\ApiController;

/**
 * 地块接口
 */
class BlockController extends ApiController {

    /**
     * 地块信息
     */
    public function blockInfo() {
        $block_id = I('post.block_id', 0);

        if (empty($block_id)) {
            apiReturn('地块ID不能为空', 300);
        }

        $blockInfo = M('Block')->field('id,sn,area,remark')->where("status=1 AND id={$block_id}")->find();

        if (!empty($blockInfo)) {
            //获取苗木分类种类信息
            $treeTypeInfo = R('Tree/getTreeTypeInfo');

            $countInfoArr = array();
            $total = 0;
            $countInfoRet = M('Tree')->field('type_id,status,COUNT(*) AS num')->where("block_id={$block_id}")->group('type_id,status')->select();
            if (!empty($countInfoRet)) {
                foreach ($countInfoRet as $countInfo) {
                    $total += $countInfo['num'];
                    if (!array_key_exists($countInfo['type_id'], $countInfoArr)) {
                        $tmp = $treeTypeInfo[$countInfo['type_id']];
                        $countInfoArr[$countInfo['type_id']] = array(
                            'large_name' => $tmp['large_name'],
                            'small_name' => $tmp['small_name'],
                        );
                    }
                    if ($countInfo['status'] == 1) {
                        $countInfoArr[$countInfo['type_id']]['save'] = $countInfo['num'];
                    } else if ($countInfo['status'] == 2) {
                        $countInfoArr[$countInfo['type_id']]['out'] = $countInfo['num'];
                    } else if ($countInfo['status'] == 3) {
                        $countInfoArr[$countInfo['type_id']]['die'] = $countInfo['num'];
                    }
                }
            }
            $blockInfo['total'] = $total;
            $blockInfo['countInfo'] = $countInfoArr;

            apiReturn('获取数据', 200, $blockInfo);
        } else {
            apiReturn('无数据', 300);
        }
    }

    /**
     * 获取地块下苗木信息
     */
    public function getTrees() {
        $pageSize = I('post.pageSize', C('PAGE_SIZE'));
        $pageCurrent = I('post.pageCurrent', 1);
        $orderField = I('post.orderField', 't.id');
        $orderDirection = I('post.orderDirection', 'DESC');
        $block_id = I('post.block_id', 0);
        $tree = M('Tree');

        if (empty($block_id)) {
            apiReturn('地块ID不能为空', 300);
        }

        //构造WHERE条件
        $where = "1=1 AND t.block_id={$block_id}";

        //数据总数
        $total = $tree->alias('t')
                ->join('LEFT JOIN __TREE_TYPE__ AS tt ON tt.id=t.type_id')
                ->join('LEFT JOIN __TREE_TYPE__ AS tt2 ON tt2.id=tt.pid')
                ->where($where)
                ->count();

        //数据列表
        $list = $tree->field('t.id,t.sn,t.row,t.col,t.status,t.is_sign,tt2.name AS large_name,tt.name AS small_name')
                ->alias('t')
                ->join('LEFT JOIN __TREE_TYPE__ AS tt ON tt.id=t.type_id')
                ->join('LEFT JOIN __TREE_TYPE__ AS tt2 ON tt2.id=tt.pid')
                ->where($where)
                ->order("{$orderField} {$orderDirection}")
                ->page("{$pageCurrent},{$pageSize}")
                ->select();

        if (!empty($list)) {
            foreach ($list as &$item) {
                $treeAttr = M('TreeAttr')->field('a.name AS attr_name,ta.attr_id,ta.attr_value,a.unit as attr_unit')
                        ->alias('ta')
                        ->join('LEFT JOIN __ATTR__ AS a ON a.id=ta.attr_id')
                        ->where("ta.tree_id={$item['id']}")
                        ->select();

                if (!empty($treeAttr)) {
                    $tmp = array();
                    foreach ($treeAttr as $attr) {
                        if (array_key_exists($attr['attr_id'], $tmp)) {
                            $tmp[$attr['attr_id']]['attr_value'] = $tmp[$attr['attr_id']]['attr_value'] . ',' . $attr['attr_value'];
                        } else {
                            $tmp[$attr['attr_id']] = $attr;
                        }
                    }
                    $treeAttr = $tmp;
                }

                $item['attr'] = $treeAttr;
            }
            unset($item);

            $data['total'] = $total;
            $data['pages'] = ceil($total / $pageSize);
            $data['list'] = $list;
            apiReturn('获取数据', 200, $data);
        } else {
            apiReturn('无数据', 300);
        }
    }

    /**
     * 获取基地下地块
     */
    public function getBaseBlock() {
        $base_id = I('post.base_id', 0);

        if (empty($base_id)) {
            apiReturn('基地ID不能为空', 300);
        }

        $list = M('Block')->field('id,sn')->where("status=1 AND base_id={$base_id}")->order('sort ASC')->select();
        if (!empty($list)) {
            apiReturn('获取数据', 200, $list);
        } else {
            apiReturn('无数据', 300);
        }
    }

}
